package com.hlx.service.admin.Impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hlx.entity.admin.Station;
import com.hlx.mapper.admin.StationMapper;
import com.hlx.service.admin.StationService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class StationServiceImpl extends ServiceImpl<StationMapper, Station> implements StationService {
    @Override
    public Station getStationByName(String stationName) {
        LambdaQueryWrapper<Station> queryWrapperS = new LambdaQueryWrapper<>();
        queryWrapperS.eq(Station::getStationName,stationName);
        Station st = this.getOne(queryWrapperS);
        return st;
    }

    @Override
    public Integer judgeBeijing(List<String> stationName) {
        Iterator it = stationName.iterator();
        Integer[] id = new Integer[2];
        for(Integer i = 0; it.hasNext(); i++){
            LambdaQueryWrapper<Station> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(Station::getStationName,it.next());
            id[i] = this.getOne(queryWrapper).getId();
        }
        if(id[0] < id[1]){
            return 1;
        }else{
            return -1;
        }
    }

    @Override
    public Integer cityDistance(List<String> stationName) {
        Iterator it = stationName.iterator();
        Integer[] dis = new Integer[2];
        for(Integer i = 0; it.hasNext(); i++){
            LambdaQueryWrapper<Station> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(Station::getStationName,it.next());
            dis[i] = this.getOne(queryWrapper).getDistanceBeijing();
        }
        if(dis[0] > dis[1]) {
            return dis[0] - dis[1];
        }else{
            return dis[1] - dis[0];
        }
    }

    @Override
    public Integer getIdByStationName(String stationName) {
        LambdaQueryWrapper<Station> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Station::getStationName,stationName);
        return this.getOne(queryWrapper).getId();
    }

    @Override
    public Integer getDistance(String stationStart, String stationEnd) {
        LambdaQueryWrapper<Station> queryWrapperStart = new LambdaQueryWrapper<>();
        queryWrapperStart.eq(Station::getStationName,stationStart);
        LambdaQueryWrapper<Station> queryWrapperEnd = new LambdaQueryWrapper<>();
        queryWrapperEnd.eq(Station::getStationName,stationEnd);
        Integer startDistance = this.getOne(queryWrapperStart).getDistanceBeijing();
        Integer endDistance = this.getOne(queryWrapperEnd).getDistanceBeijing();
        if(startDistance > endDistance){
            return startDistance - endDistance;
        }else {
            return endDistance - startDistance;
        }
    }

    @Override
    public List<Station> getAllStationByName(List<String> stationNames) {
        LambdaQueryWrapper<Station> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Station::getStationName,stationNames);
        return this.list(queryWrapper);
    }
}
